const app = getApp()
const util = require('../../utils/util.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        deposit: '0.00',
        scrollHeight: '',
        scrollHeightSon: '',
        currtab: 0,
        data_balance: [], //余额数据
        refreshData: {
            page: 1,
            size: 10,
            isEnd: false
        }, //分页信息
        canCommit: false,

    },
    /**
     * 计算滚动的可用高度
     */
    computeScrollViewHeight() {
        let that = this;
        let query = wx.createSelectorQuery().in(this);
        query.select('.card-header').boundingClientRect();
        query.select('.card-balance').boundingClientRect();
        query.exec(res => {
            let searchHeight = res[0].height;
            let balanceHeight = res[1].height;
            let windowHeight = wx.getSystemInfoSync().windowHeight;
            let scrollHeight = windowHeight - searchHeight;
            let scrollHeightSon = windowHeight - searchHeight - balanceHeight;
            this.setData({
                scrollHeight: scrollHeight,
                scrollHeightSon: scrollHeightSon
            });
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        //请求分享数据
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=wx_share&op=get_share',
            data: {},
            method: 'GET',
            dataType: 'json',
            success: res => {
                if (res.data.code == '200') {
                    this.setData({
                        share_default_title: res.data.datas.title,
                        share_default_image_url: res.data.datas.imageUrl
                    })
                }
            }
        })
        util.share();
        let self = this;
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    member_key: res.data
                });
                this.getBalance();
                this.getData();
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        //list滚动的高度
        this.computeScrollViewHeight();
        //初始化图形验证码
        // this.check = this.selectComponent("#check");
        // this.check.drawPic();
    },

    /**
     * tab点击事件
     */
    tabClick: function (event) {
        var self = this;
        var tab = event.currentTarget.dataset.tab;
        self.setData({
            currtab: tab,
        });
    },
    /**
     * 获取余额
     */
    getBalance: function () {
        var self = this;
        //获取余额
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_index&op=my_asset&key=' + self.data.member_key + '&fields=available_rc_balance',
            success(res) {

                self.setData({
                    deposit: res.data.datas.available_rc_balance
                });
            }
        });
    },
    /**
     * 获取数据
     */
    getData: function () {
        var self = this;
        if (self.data.refreshData.isEnd) {
            return;
        }
        //获取列表数据
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_fund&op=rcblog&key=' + self.data.member_key + '&curpage=' + self.data.refreshData.page + '&page=' + self.data.refreshData.size,
            success(res) {
                let tempData = [];
                //如果是第一页数据只添加新的数据 
                self.data.refreshData.page > 1 ? tempData = self.data.data_balance.concat(res.data.datas.log_list) : tempData = res.data.datas.log_list;
                self.setData({
                    data_balance: tempData, //获取数据数组   
                });
                if (!res.data.hasmore) {
                    self.data.refreshData.isEnd = true; //把“没有数据”设为true，显示  
                    self.setData({
                        refreshData: self.data.refreshData
                    });
                }
            },
            fail: function () {
                wx.showToast({
                    title: '数据加载失败',
                    icon: 'none'
                })
            },
        });
    },
    /*
     *监听充值卡的输入
     */
    cardInput: function (event) {
        var self = this;
        if (event.detail.value.length > 0) {
            self.data.canCommit = true;
        } else {
            self.data.canCommit = false;
        }
        self.setData({
            canCommit: self.data.canCommit
        });
    },
    /**
     * 卡片提交
     */
    commitCard: function (event) {
        var self = this;
        if (self.data.canCommit) {
            self.check.drawPic();
            self.check.showDialog(event.detail);
        }
    },
    //图像验证码确认事件
    _vertifySuccess(detail) {
        this.rechageSubmit(detail.detail.value.cardn);
    },
    //禁止swiper左右切换
    stopTouchMove: function () {
        return false;
    },
    //充值提交
    rechageSubmit: function (cardN) {
        wx.showLoading({
            title: "提交中",
        })
        var self = this;
        wx.request({
            url: app.globalData.ApiUrl + '/index.php?act=member_fund&op=rechargecard_add',
            method: "POST",
            data: {
                key: self.data.member_key,
                rc_sn: cardN,
            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            success: function (res) {
                if (res.data.code == 200) {
                    self.getBalance();
                    self.setData({
                        currtab: 0
                    });
                    self.data.refreshData.page = 1;
                    self.data.refreshData.isEnd = false;
                    self.getData();
                    wx.showToast({
                        title: '充值成功',
                        icon: 'success'
                    })
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none'
                    })
                }
            },
            fail: function (res) {
                wx.showToast({
                    title: '网络请求失败',
                    icon: 'none'
                })
            },
            complete: function (res) {
                //wx.hideLoading();
            }
        })
    },
    //滚动到底部触发事件  
    searchScrollLower: function () {
        let that = this;
        if (!that.data.isEnd) {
            that.data.refreshData.page = that.data.refreshData.page + 1;
            that.getData();
        }
    },
    onShareAppMessage: function () {
        let share_key = this.data.member_key ? this.data.member_key : '';
        let share_path = '';
        if (util.getCurrentPageUrlWithArgs().indexOf("?") != -1) {
            share_path = util.getCurrentPageUrlWithArgs() + '&share_key=' + share_key;
        } else {
            share_path = util.getCurrentPageUrlWithArgs() + '?share_key=' + share_key;
        }
        return {
            title: this.data.share_default_title,
            imageUrl: this.data.share_default_image_url,
            path: share_path
        }
    }
})